package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Punishment;
import com.example.exception.BusinessException;
import com.example.mapper.PunishmentMapper;
import com.example.service.PunishmentService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author HP
* @description 针对表【punishment】的数据库操作Service实现
* @createDate 2025-08-18 19:33:15
*/
@Service
public class PunishmentServiceImpl extends ServiceImpl<PunishmentMapper, Punishment>
    implements PunishmentService {

    @Resource
    private PunishmentMapper punishmentMapper;

    @Override
    public boolean addPunishment(Punishment punishment) {
        if (punishment == null) {
            throw new BusinessException("惩罚信息不能为空");
        }
        if (punishment.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
        if (punishment.getReason() == null || punishment.getReason().trim().isEmpty()) {
            throw new BusinessException("惩罚原因不能为空");
        }
        if (punishment.getPunishmentType() == null) {
            throw new BusinessException("惩罚类型不能为空");
        }
        int insert = punishmentMapper.insert(punishment);
        log.error("用户处罚完成！");
        if (insert <= 0) {
            throw new BusinessException("添加惩罚记录失败");
        }
        return true;
    }

    @Override
    public boolean updatePunishment(Punishment punishment) {
        if (punishment == null || punishment.getId() == null) {
            throw new BusinessException("惩罚ID不能为空");
        }

        // 检查惩罚记录是否存在
        Punishment existingPunishment = punishmentMapper.selectById(punishment.getId());
        if (existingPunishment == null) {
            throw new BusinessException("惩罚记录不存在");
        }

        // 验证更新信息
        if (punishment.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
        if (punishment.getReason() != null && punishment.getReason().trim().isEmpty()) {
            throw new BusinessException("惩罚原因不能为空");
        }
        if (punishment.getPunishmentType() == null) {
            throw new BusinessException("惩罚类型不能为空");
        }
        if (punishment.getAmount() != null && punishment.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException("惩罚金额必须大于0");
        }

        int update = punishmentMapper.updateById(punishment);
        if (update <= 0) {
            throw new BusinessException("更新惩罚记录失败");
        }
        return true;
    }

    @Override
    public boolean deletePunishment(Long id) {
        if (id == null) {
            throw new BusinessException("惩罚ID不能为空");
        }

        // 检查惩罚记录是否存在
        Punishment punishment = punishmentMapper.selectById(id);
        if (punishment == null) {
            throw new BusinessException("惩罚记录不存在");
        }

        int delete = punishmentMapper.deleteById(id);
        if (delete <= 0) {
            throw new BusinessException("删除惩罚记录失败");
        }
        return true;
    }

    @Override
    public List<Punishment> getPunishmentsByUserId(Long userId) {
        if (userId == null) {
            throw new BusinessException("用户ID不能为空");
        }

        QueryWrapper<Punishment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("create_time");
        List<Punishment> punishments = punishmentMapper.selectList(queryWrapper);
        return punishments;
    }

    @Override
    public List<Punishment> getUnprocessedPunishments() {
        QueryWrapper<Punishment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("punishment_status", 0); // 0:处罚中
        queryWrapper.orderByDesc("created_at");
        List<Punishment> punishments = punishmentMapper.selectList(queryWrapper);
        return punishments;
    }

    @Override
    public boolean processPunishment(Long id, Long handlerId, String remark) {
        if (id == null) {
            throw new BusinessException("惩罚ID不能为空");
        }
        if (handlerId == null) {
            throw new BusinessException("处理人ID不能为空");
        }

        // 检查惩罚记录是否存在
        Punishment punishment = punishmentMapper.selectById(id);
        if (punishment == null) {
            throw new BusinessException("惩罚记录不存在");
        }

        // 检查是否已经处理过
        if (punishment.getPunishmentStatus() != 0) {
            throw new BusinessException("该惩罚记录已处理或已撤销");
        }

        // 标记为已结束
        punishment.setPunishmentStatus(1); // 1:已结束
        punishment.setEndTime(new Date());
        // 可以添加处理人ID和备注字段，如果实体类中有这些字段
        // punishment.setHandler_id(handlerId);
        // punishment.setRemark(remark);

        int update = punishmentMapper.updateById(punishment);
        if (update <= 0) {
            throw new BusinessException("处理惩罚记录失败");
        }
        return true;
    }
}




